﻿using RayDen.RayEngine.Core;
using RayDen.RayEngine.Core.Devices;
using RayDen.RayEngine.Core.Interface;
using RayDen.RayEngine.Core.Threads;
using RayDen.RayEngine.Managers;
using RayDen.RayEngine.Samplers;

namespace RayDen.RayEngine.Engines.PhotonMapping {
    public class PhotonMapRenderer : BaseRenderingEngine {
       

        public override bool PreprocessRequired
        {
            get { return true; }
        }

        public override void Preprocess(RayEngineScene scene, IntersectionEngine ie)
        {
            this.Context.BuildMaps(ie, scene);
        }


        public PhotonMapRenderer(RayEngineScene scene) : base(scene) {
            LowLatencySampling = false;
        }

        public override RenderThread CreateRenderThread(int threadIndex, IntersectionEngine intersectionDevice,
                                                        RayEngineScene scene, IImageFilm film, IFrameManager frameManager, SamplingContext samplingContext)
        {
            if (this.Context == null)
            {
                Context = new PhotonMappingContext() { Film = film, surfaceSampler = (SurfaceSampler)samplingContext.SurfaceSampler };
            }
            return new PhotonMappingRenderThread(threadIndex, intersectionDevice, scene, LowLatencySampling, film, Context, samplingContext ,frameManager);
        }

        public override ISampler CreateSampler(int width, int height, int maxPathDepth, int threadIndex, int screenStartLine, int windowHeight) {

            //return new PhotonMappingContextSampler() { screenWidth = width, screenHeight = height*4, lowLatency = LowLatencySampling, screenStartLine = screenStartLine, tIdx = threadIndex };
            return SamplerFactory.Instance.CreateSampler(width, height, maxPathDepth, screenStartLine, LowLatencySampling,threadIndex, windowHeight);
        }

        public override Core.Interface.IImageFilm CreateFilm(int width, int height)
        {
            
            return new ImageFilm<PhotonMappingPathSampler>(width, height);
        }

        private PhotonMappingContext Context;

    }
}
